//
// Created by grainlee on 16/9/12.
//

#include <iostream>
#include <vector>

using namespace std;
int threeSumClosest(vector<int>& nums, int target) {
    int res;
    int closest = 0x7fffffff;


    sort(nums.begin(),nums.end());
    for (int i = 0 ; i < nums.size()-1; i++){
        if (i > 0 && nums[i] == nums[i-1]) continue;
        int j,k;
        j = i+1;
        k = nums.size() -1;

        while (j < k){
            if ( j > i+1 && nums[j] == nums[j-1]){
                j++;
                continue;
            }
            if (k < nums.size()-1 && nums[k] == nums[k+1]){
                k--;
                continue;
            }
            int num = nums[i] + nums[j] + nums[k];
            int tmp = abs(num-target);
            if (tmp < closest){
                closest = tmp;
                res = num;
            }


            if (num > target){
                k--;
            } else if (num < target){
                j++;
            } else{
                return target;
            }
        }
    }
    return res;
}

int main(){


}
